package com.geeguo.ebuilder.client.base.database.plugin;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.geeguo.ebuilder.client.base.tenant.model.DataContext;
import com.geeguo.ebuilder.client.base.tenant.utils.DataContextHolder;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

@Component
public class MybatisPlusMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        Object tenantCode = this.getFieldValByName("tenantCode", metaObject);
        if (tenantCode == null) {
            DataContext dataContext = DataContextHolder.getDataContext();
            if (dataContext != null) {
                this.setFieldValByName("tenantCode", dataContext.getTenantCode(), metaObject);
            }
        }
        Object createTime = this.getFieldValByName("createTime", metaObject);
        Object modifyTime = this.getFieldValByName("modifyTime", metaObject);
        if (createTime != null && modifyTime == null) {
            this.setFieldValByName("modifyTime", createTime, metaObject);
        }
        Object createUser = this.getFieldValByName("createUser", metaObject);
        Object modifyUser = this.getFieldValByName("modifyUser", metaObject);
        if (createUser != null && modifyUser == null) {
            this.setFieldValByName("modifyUser", createUser, metaObject);
        }
    }
    
    @Override
    public void updateFill(MetaObject metaObject) {

    }
}
